import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_picker/flutter_picker.dart';
import 'package:material_dialogs/dialogs.dart';
import 'package:material_dialogs/shared/types.dart';
import 'package:tracker/common/log/x_logger.dart';
import 'package:tracker/common/model/options_item.dart';
import 'package:tracker/pages/home/setting/dialog/command_dialog_interface.dart';
import 'package:tracker/pages/home/setting/model/remote_setting_list_resp.dart';
import 'package:tracker/theme/app_theme.dart';
import 'package:tracker/utils/toast_utils.dart';

class CustomCommandDialog implements CommandDialogInterface {
  final GlobalKey<ScaffoldState> scaffoldKey;
  final RemoteSettingData remoteSettingData;

  // 回调函数
  final Function(String cmdContent)? onConfirm;

  CustomCommandDialog({
    required this.scaffoldKey,
    required this.remoteSettingData,
    this.onConfirm,
  });

  TextEditingController textEditingController = TextEditingController();

  @override
  void show(BuildContext context) {
    Dialogs.materialDialog(
        context: context,
        customView: StatefulBuilder(builder: (newContext, newSetState) {
          return Container(
            padding: const EdgeInsets.only(left: 20, right: 20, top: 20),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  remoteSettingData.cmdName ?? '',
                  style: TextStyle(
                    fontSize: 18,
                    color: AppTheme.themeConfig.primaryTextColor,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                const SizedBox(
                  height: 20,
                ),
                Row(
                  children: [
                    // 提示语
                    Expanded(
                      child: Text(
                        remoteSettingData.remark ?? '',
                        style: TextStyle(
                          fontSize: 16,
                          color: AppTheme.themeConfig.infoTextColor,
                        ),
                      ),
                    ),
                  ],
                ),
                SizedBox(
                  height: 12,
                ),
                Row(
                  children: [
                    // 文本框
                    Expanded(
                      child: TextField(
                        controller: textEditingController,
                        maxLines: 3,
                        decoration: InputDecoration(
                          hintText: '请输入指令内容',
                          hintStyle: TextStyle(
                            fontSize: 16,
                            color: AppTheme.themeConfig.infoTextColor,
                          ),
                          border: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(5),
                            borderSide: BorderSide(
                              color: AppTheme.themeConfig.infoTextColor,
                              width: 1,
                            ),
                          ),
                          // 背景色
                          fillColor: Colors.white,
                        ),
                        onChanged: (value) {
                          updateCmdContent(value);
                        },
                      ),
                    ),
                  ],
                )
              ],
            ),
          );
        }),
        customViewPosition: CustomViewPosition.BEFORE_TITLE,
        actions: [
          TextButton(
            onPressed: () {
              Navigator.pop(context);
            },
            child: const Text(
              '取消',
              style: TextStyle(color: Colors.grey, fontSize: 15),
            ),
          ),
          TextButton(
            onPressed: () {
              // 指令不能为空，且字符长度不能超过100
              if (getCommand().isEmpty) {
                ToastUtils.showToast('指令不能为空');
                return;
              }
              if (getCommand().length > 100) {
                ToastUtils.showToast('指令长度不能超过100');
                return;
              }
              // 发送指令
              Navigator.pop(context);
              onConfirm?.call(getCommand());
            },
            child: Text(
              '发送指令',
              style: TextStyle(color: AppTheme.themeConfig.primaryColor, fontSize: 15),
            ),
          ),
        ]);
  }

  @override
  String getCommand() {
    // 拼接指令内容
    return textEditingController.text.trim();
  }

  @override
  void updateCmdContent(String cmdContent) {
    // TODO: implement updateCmdContent
  }
}
